﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.BLL.OverOrderBLL;
using MINERP.MODEL.OverOrder;
using MINERP.COM;
using System.Windows.Forms;
using MINERP.MODEL.FoundationModel;
using System.Data;

namespace MINERP.OverOrder.Presenter
{
    public class FrmOverOrderPresenter
    {
        private OverOrderBLL _bll;
        private IView.IFrmOverOrder _view;

        private List<OverOrderView> _shopTaskView;//厂商的任务列表数据---内存数据
        private List<OverOrderView> _mainView;//主任务列表数据---内存数据
        private List<OutProcessShop_Info> _outShopView;//能够打工选定产品的委外厂商列表
        private List<int> _PreSelectedOrderProductIdList;//上面数据（_outShopView）对应的产品ID

        public FrmOverOrderPresenter(IView.IFrmOverOrder view)
        {
            _view = view;
            _bll = new OverOrderBLL();
            GetMainTask();
        }

        /// <summary>
        /// 是示厂商列表
        /// </summary>
        public void ShowOutShopView()
        {
            List<int> newOrderProductIdList = _view.SelectedOrderProductidList;


            _outShopView = _bll.GetOutShops(newOrderProductIdList);


            _view.ShowOutShopView(_outShopView);

            //_view.SelectedOrderProductidList.Clear();
        }
        public int GetFristoutShopID()
        {
            List<int> newOrderProductIdList = _view.SelectedOrderProductidList;

            List<OutProcessShop_Info> list = _bll.GetOutShops(newOrderProductIdList);
            //_view.SelectedOrderProductidList.Clear();
            if (list.Count > 0)
            {
                return list.First().OutShopId;
            }
            else
            {
                return 0;
            }
        }



        /// <summary>
        /// 列表中的ID是否是所有任务列表中加急级别最高的
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <returns></returns>
        private bool IsHaveAllHighPriority(List<int> orderProductList)
        {
            List<int> SelectPriorityList = _mainView.Where(oo => orderProductList.Contains(oo.OrderProductId)).Select(oov1 => oov1.Priority).Distinct().ToList();
            int LowPriorityProductId = SelectPriorityList.Min();
            List<int> allPriorityList = _mainView.Select(o => o.Priority).Distinct().ToList();
            int result = allPriorityList.Where(p => p > LowPriorityProductId && !SelectPriorityList.Contains(p)).Count();
            if (result == 0)
            {
                return true;
            }
            else//集合中有不是加急级别最高的定单
            {
                return false;
            }
        }



        /// <summary>
        /// 委外
        /// </summary>
        public void AssignOutTask()
        {
            List<int> AssignOutlList = _view.SelectedOrderProductidList;
            if (!IsHaveAllHighPriority(AssignOutlList))
            {
                MessageBox.Show("请先分配加急级别最高的定单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int operateId = _view.OperateUserId;
            int outShopId = _view.SelectedOutShopId;
            if (AssignOutlList == null || AssignOutlList.Count < 1 || outShopId < 1)
            {
                MessageBox.Show("请选择要委外的产品和委外加工商", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                try
                {
                    _bll.AssignOutTask(AssignOutlList, outShopId, operateId, _view.InOutRemark);
                    GetMainTask();
                    // SelectOutShop();
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
            //_view.SelectedOrderProductidList.Clear();
        }

        /// <summary>
        /// 从委外厂商取消操作
        /// </summary>
        public void CancelTask()
        {
            if (DialogResult.OK == MessageBox.Show("确定要取消选中的委外产品吗？", "确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                //List<int> cancelList = _view.SelectedOrderProductidList;
                List<int> cancelList = _view.SelectedCancelProductList;

                if (cancelList == null || cancelList.Count < 1)
                {
                    MessageBox.Show("请选择要结单的产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    try
                    {
                        int result = _bll.CancelOutShopTask(cancelList);
                        if (result == -1)
                        {
                            MessageBox.Show("取消失败，请与相关人员联系!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else if (result == 0)
                        {
                            MessageBox.Show("不能取消，因为该任务正在外加工中或是已经取件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            GetMainTask();
                            SelectOutShop();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }

                //_view.SelectedOrderProductidList.Clear();
            }
        }
        /// <summary>
        /// 从委外厂商取消操作
        /// </summary>
        public void CancelTask1()
        {
            if (DialogResult.OK == MessageBox.Show("确定要取消选中的委外产品吗？", "确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                List<int> cancelList = _view.SelectedOrderProductidList;
                //List<int> cancelList = _view.SelectedCancelProductList;

                if (cancelList == null || cancelList.Count < 1)
                {
                    MessageBox.Show("请选择要结单的产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    try
                    {
                        int result = _bll.CancelOutShopTask(cancelList);
                        if (result == -1)
                        {
                            MessageBox.Show("取消失败，请与相关人员联系!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else if (result == 0)
                        {
                            MessageBox.Show("不能取消，因为该任务正在外加工中或是已经取件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            MessageBox.Show("取消成功，请重新分配委外商!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            //GetMainTask();
                            //SelectOutShop();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }

                //_view.SelectedOrderProductidList.Clear();
            }
        }
        /// <summary>
        /// 选择厂商,刷新厂商任务列表
        /// </summary>
        public void SelectOutShop()
        {
            int shopId = _view.SelectedOutShopId;
            if (shopId >= 1)
            {
                if (_shopTaskView == null)
                {
                    _shopTaskView = new List<OverOrderView>();
                }

                _shopTaskView = _bll.GetShopTaskViewByOutShopIdForList(shopId);
                _view.ShowShopTaskView(_shopTaskView);
            }
            else
            {
                _view.ShowShopTaskView(new List<OverOrderView>());
            }
        }

        /// <summary>
        /// (单独)结单---在任务列表中选择某些任务然后再点击结单操作
        /// </summary>
        public void OverOrder()
        {
            if (_view.SelectedOrderProductidList.Count < 1)
            {
                MessageBox.Show("请选定要结点的产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                try
                {
                    _bll.OverOrder(_view.SelectedOrderProductidList);
                    //_view.SelectedOrderProductidList.Clear();
                    GetMainTask();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        /// <summary>
        /// 收件并结单
        /// </summary>
        public void FetchAndOrderOrder()
        {
            if (_view.SelectedOrderProductidList.Count < 1)
            {
                MessageBox.Show("请选定要结单的产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                try
                {
                    _bll.FetchAndOrderOrder(_view.SelectedOrderProductidList, _view.InFetchRemark, _view.OperateUserId);
                    //_view.SelectedOrderProductidList.Clear();
                    GetMainTask();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        /// <summary>
        /// 得到主任务列表
        /// </summary>
        public void GetMainTask()
        {
            OrderInfoStatus status = (OrderInfoStatus)_view.SelectedOrderInfoStatus;
            if (_mainView == null)
            {
                _mainView = new List<OverOrderView>();
            }
            //_mainView = _bll.GetMainTaskByStatus(status);
            _mainView = _bll.GetMainTasks(_view.CurTechId, status);
            _view.ShowMainView(_mainView);

            if (_mainView == null || _mainView.Count < 1)
            {
                //_view.ShowMainView(_mainView);
            }
            else
            {
                //_view.ShowMainView(_mainView);
                FillFilterCMB();
            }

        }
        public DataTable GetOutingTask()
        {
            
            return _bll.GetOuttingTask();
        }


        /// <summary>
        /// 填充快递筛选的下拉列表
        /// </summary>
        private void FillFilterCMB()
        {
            //List<Tb_OrderBag_Info> listOrderBag = (from p in _mainView select p.OrderBagInfo).Distinct(new ComparTb_OrderBag_Info()).OrderBy(p => p.Priority).ToList();

            ////List<string> listShopName = _mainView.Select(p => p.OrderBagInfo.ShopName).Distinct().ToList();
            //List<string> listShopName = _mainView.Select(p => p.OrderBagInfo.ShopName).Distinct().ToList();

            //List<OrderInfo_Info> litOrderInfo = _mainView.Select(p => p.OrderProductInfo).Distinct().ToList();
            //List<Tb_Product_Info> listProduct = _mainView.Select(p => p.ProductInfo).Distinct().ToList();
            //List<OutProcessRecord_Info> listOutRecord = _mainView.Select(p => p.OutRecordInfo).Distinct().ToList();
            //List<OutProcessShop_Info> listOutShop = listOutRecord.Select(o => o.OutShopInfo).Distinct(new ComparOutProcessShop_Info()).ToList();

            //_view.FillQuickFilterCOMB(listOrderBag, litOrderInfo, listProduct, listOutRecord, listOutShop, listShopName);


        }

        #region 快速筛选
        public void FilterOrderBag()
        {
            if (_view.FilterOrderBagId > 0)
            {
                _view.ShowMainView(_mainView.Where(p => p.OrderProductId == _view.FilterOrderBagId).ToList());
            }
        }

        public void FilterShop()
        {
            if (_view.FilterShopName != "")
            {
                if (_view.FilterShopName == "全部")
                {
                    _view.ShowMainView(_mainView);

                }
                else
                {
                    _view.ShowMainView(_mainView.Where(p => p.OrderBagInfo.ShopName == _view.FilterShopName).ToList());
                }
            }
        }

        public void FilterCtmName()
        {
            if (_view.FilterCtmName != "")
            {
                if (_view.FilterCtmName == "全部")
                {
                    _view.ShowMainView(_mainView);

                }
                else
                {
                    _view.ShowMainView(_mainView.Where(p => p.CtmName == _view.FilterCtmName).ToList());

                }
            }
        }

        public void FilterPriority()
        {
            if (_view.FilterPriority > 0)
            {
                _view.ShowMainView(_mainView.Where(p => p.Priority == _view.FilterPriority).ToList());
            }
        }

        public void FilterProduct()
        {
            if (_view.FilterProductName != "")
            {
                if (_view.FilterProductName == "全部")
                {
                    _view.ShowMainView(_mainView);

                }
                else
                {
                    _view.ShowMainView(_mainView.Where(p => p.ProductName == _view.FilterProductName).ToList());

                }
            }
        }

        public void FilterEndOutTime()
        {
            if (_view.FilterEndOutTime != null && _view.FilterStartOutTime != null)
            {
                _view.ShowMainView(_mainView.Where(p => p.CreateTime <= _view.FilterEndOutTime.AddDays(1) && p.CreateTime >= _view.FilterStartOutTime).ToList());
            }
        }

        public void FilterStartOutTime()
        {
            if (_view.FilterStartOutTime != null && _view.FilterStartOutTime.Year != 1)
            {
                _view.ShowMainView(_mainView.Where(p => p.CreateTime >= _view.FilterStartOutTime).ToList());
            }
        }

        public void FilterStartPreFetchTime()
        {
            if (_view.FilterStartPreFetchTime != null && _view.FilterStartPreFetchTime.Year != 1)
            {
                _view.ShowMainView(_mainView.Where(p => p.PreFetchTime >= _view.FilterStartPreFetchTime).ToList());
            }
        }

        public void FilterEndPreFetchTime()
        {
            if (_view.FilterEndPriFetchTime != null && _view.FilterStartPreFetchTime != null)
            {
                _view.ShowMainView(_mainView.Where(p => p.PreFetchTime >= _view.FilterStartPreFetchTime && p.PreFetchTime <= _view.FilterEndPriFetchTime.AddDays(1)).ToList());
            }
        }

        public void FilterOutOperateUser()
        {
            if (_view.OutOperateUser != "")
            {
                if (_view.OutOperateUser.Equals("全部"))
                {
                    _view.ShowMainView(_mainView);

                }
                else
                {
                    _view.ShowMainView(_mainView.Where(p => p.CreateUserName == _view.OutOperateUser).ToList());

                }
            }
        }

        /// <summary>
        /// 查看缓存中全部的“正在委外加工”的产品列表
        /// </summary>
        public void ShowHCView()
        {
            _view.ShowMainView(_mainView);
        }
        public List<OutProcessShop_Info> GetOutShop()
        {
            return _bll.GetOutShopList();
        }


        public void FilterOutShop()
        {
            if (_view.OutShopId >= 0)
            {
                if (_view.OutShopId == 0)
                {
                    _view.ShowMainView(_mainView);
                }
                else
                {
                    _view.ShowMainView(
                        _mainView.Where(p => p.OutRecordInfo.OutShopInfo.OutShopId == _view.OutShopId).ToList());
                }

            }
        }
    }

        #endregion


    #region help methods
    public class ComparTb_OrderBag_Info : IEqualityComparer<Tb_OrderBag_Info>
    {

        public bool Equals(Tb_OrderBag_Info x, Tb_OrderBag_Info y)
        {
            if (x.OrderBagId == y.OrderBagId)
                return true;
            else return false;
        }

        public int GetHashCode(Tb_OrderBag_Info obj)
        {
            return obj.ToString().GetHashCode();
        }
    }

    public class ComparOutProcessShop_Info : IEqualityComparer<OutProcessShop_Info>
    {

        public bool Equals(OutProcessShop_Info x, OutProcessShop_Info y)
        {
            if (x.OutShopId == y.OutShopId)
                return true;
            else return false;
        }

        public int GetHashCode(OutProcessShop_Info obj)
        {
            return obj.ToString().GetHashCode();
        }
    }
    #endregion
    
}

